Shader "Converted/Cloud2d"
{
    Properties
    {
        [Toggle]_AA("AA",int) = 0

        _BB("BB",Range(0,1)) = 1
        _CC("CC",Range(0,1)) = 1
        _TC("_TC",Range(0,1)) = 1
 
        _Mouse ("Mouse", Vector) = (0.5, 0.5, 0.5, 0.5)
        [ToggleUI] _GammaCorrect ("Gamma Correction", Float) = 1
        _Resolution ("Resolution (Change if AA is bad)", Range(1, 1024)) = 1
        
        // 云层纹理相关
        _CloudTexture ("Cloud Texture", 2D) = "white" {}
        _CloudTextureThreshold ("Cloud Texture Threshold", Range(0, 1)) = 0.5
        _CloudTextureSpeed ("Cloud Texture Speed", Vector) = (0.1, 0.1, 0, 0)
        _CloudScale("_CloudScale", Range(0,10)) = 1
        _CloudSteps ("Cloud Steps", Range(5, 10)) = 10
        _CloudMarchDist ("Cloud March Distance", Range(0.1, 1)) = 0.35
        _CloudAmbient ("Cloud Ambient", Range(0, 10)) = 10
        _NoiseTex ("_NoiseTex", 2D) = "white" {}
        // 颜色相关属性
       _SkyColorTop ("Sky Color Top", Color) = (0.1, 0.5, 0.9, 1)
         _SkyColorBottom ("Sky Color Bottom", Color) = (0.1, 0.1, 0.9, 1)
        [HDR]_CloudColor ("Cloud Color", Color) = (1, 1, 1, 1)
 
          _SunColor ("Sun Color", Color) = (1, 1, 1, 1)
        _SunIntensity ("Sun Intensity", Range(0, 0.01)) = 0.002
        _SunPower ("Sun Power", Range(0, 5)) = 1.7
    }
    SubShader
    {
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };

            float _AA;
            float _BB;
            // Built-in properties
            float4 _Mouse;
            float _GammaCorrect;
            float _Resolution;

            // 云层相关属性
            sampler2D _CloudTexture;
            sampler2D _NoiseTex;
            float4 _CloudTexture_ST;
            float4 _CloudTextureSpeed;
            float _CloudTextureThreshold;
            float _CloudSteps;
            float _CloudMarchDist;
            float _CloudAmbient;

            // 颜色相关属性
            float4 _SkyColorTop;
            float4 _SkyColorBottom;
            float4 _CloudColor;
     
            float4 _SunColor;
            float _SunIntensity;
            float _SunPower;
            float _TC;
 
            // GLSL Compatability macros
            #define glsl_mod(x,y) (((x)-(y)*floor((x)/(y))))
            #define iResolution float3(_Resolution, _Resolution, _Resolution)

            #define SAT(x) clamp(x, 0., 1.)

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv =  v.uv;
                return o;
            }

            float clouds(float2 uv, float t)
            {
                float2 cloudUV = uv * _CloudTexture_ST.xy + _CloudTexture_ST.zw;
         
                float cloudValue = tex2D(_CloudTexture, cloudUV+_CloudTextureSpeed.xy * t).r;
 
                cloudValue*=_TC;
                //return cloudValue;
               //float cloudValue2 = smoothstep(0,0.7,cloudValue - _CloudTextureThreshold);
               //return cloudValue*cloudValue2;
                //float cloudValue2 = tex2D(_NoiseTex, cloudUV  +_CloudTextureSpeed.zw * t).r;
                //cloudValue*= lerp(0.8,1.0,cloudValue2);
                return max(0, cloudValue - _CloudTextureThreshold);
            }
            float _CloudScale;
            float4 frag (v2f __vertex_output) : SV_Target
            {
                v2f vertex_output = __vertex_output;
                float4 fragColor = 0;
                float2 fragCoord = vertex_output.uv * _Resolution;
                float2 uv = fragCoord/iResolution.y;
                float2 m = _Mouse.xy/iResolution.y;
                float t = _Time.y;
                t = 0;
                float2 marchDist = ((float2)_CloudMarchDist*max(iResolution.x, iResolution.y))/iResolution.xy;
                float stepsInv = 1./_CloudSteps;
                float aoPower = _CloudAmbient/_CloudSteps;
                float2 sunDir = normalize(m-uv)*marchDist*stepsInv;

                float2 marchUv = vertex_output.uv*_CloudScale;
                float cloudColor = 1.;
                float cloudShape = clouds(marchUv, t);
              
                 for (float i = 0.;i<marchDist.x; i += marchDist.x*stepsInv)
                {
                    marchUv += sunDir*i;
                    float c = clouds(marchUv, t);
                    cloudColor *= clamp(1.-c*aoPower, 0., 1.);
                } 
                 

                 
                cloudColor = exp(-cloudColor)*(1.-exp(-cloudColor*2.))*2.;
                cloudColor *= cloudShape;

              
                float3 skyCol = lerp(_SkyColorBottom.rgb, _SkyColorTop.rgb, uv.y);
                float3 col = ((float3)0.);
                col = skyCol+cloudShape;
                
             
                float3 cloudCol = _CloudColor.rgb * cloudColor  ;
                //if(_AA)
                col = lerp(cloudCol, col, 1.-cloudShape);
                float sun = _SunIntensity/pow(length(uv-m), _SunPower);
                col += (1.-smoothstep(0., 0.4, cloudShape))*sun*_SunColor.rgb;
                fragColor = float4(sqrt(col), 1.);
                if (_GammaCorrect) fragColor.rgb = pow(fragColor.rgb, 2.2);
                return fragColor;
            }
            ENDCG
        }
    }
}
